<template>
  <dialog-component
    :dialog-visible="state.show"
    size="360px"
    title="报表导出"
    :btn-text="'导出'"
    @close-dialog="handleClose()"
    @save-dialog="handleSubmit()"
  >
    <el-form ref="formRef" :model="state.formData" :rules="state.rules" label-width="auto">
      <el-form-item label="报表名称:"> {{ title }} </el-form-item>
      <el-form-item label="报表密码:" prop="password">
        <el-input v-model="state.formData.password" placeholder="请输入报表密码或直接导出"></el-input>
      </el-form-item>
    </el-form>
  </dialog-component>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { useProxy } from '@/utils/proxy'
import { exportTableData, exportRepayTableData } from '../api'
import { downLoadTemplate } from '@/utils/download'
import { validCode } from '@/utils/validate'
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  userType: {
    type: String,
    default: '0'
  },
  areaCodeList: {
    type: Array,
    default: () => []
  },
  months: {
    type: String,
    default: ''
  },
  tableTitle: {
    type: String,
    default: ''
  }
})
const formRef = ref<FormInstance>()
const { proxy } = useProxy()
const state = reactive({
  isSubmiting: false,
  tableId: '',
  show: false,
  formData: {
    password: ''
  },
  rules: {
    password: [{ validator: validCode, message: '密码只能输入英文和数字', trigger: 'blur' }]
  } as FormRules
})
// 关闭回调
const handleClose = () => {
  state.show = false
  state.tableId = ''
  state.formData = {
    password: ''
  }
}
// 提交
const handleSubmit = async () => {
  await formRef.value?.validate(async (valid) => {
    if (valid) {
      if (state.isSubmiting) return
      state.isSubmiting = true
      let allArea = false
      let areaCorporations = [] as any[]
      if (props.areaCodeList.length > 0) {
        if (props.userType == '1') {
          let parentObj = props.areaCodeList.filter((v: any) => v.level == 1)[0] as any
          if (parentObj.expend) {
            allArea = true
            props.areaCodeList.forEach((v: any) => {
              if (v.expend && v.level != 1 && !v?.corporationCode) {
                let childArr = props.areaCodeList.filter(
                  (item: any) => item.expend && item.parentAreaCode == v.areaCode
                )
                if (childArr.length > 0) {
                  areaCorporations.push({
                    areaCode: v.areaCode,
                    corporationCodes: childArr.map((v: any) => v?.corporationCode)
                  })
                } else {
                  areaCorporations.push({
                    areaCode: v.areaCode,
                    corporationCodes: []
                  })
                }
              }
            })
          }
        } else {
          props.areaCodeList.forEach((v: any) => {
            if (v.expend && !v?.corporationCode) {
              let childArr = props.areaCodeList.filter(
                (item: any) => item.expend && item?.parentAreaCode == v.areaCode
              )
              if (childArr.length > 0) {
                areaCorporations.push({
                  areaCode: v.areaCode,
                  corporationCodes: childArr.map((v: any) => v?.corporationCode)
                })
              } else {
                areaCorporations.push({
                  areaCode: v.areaCode,
                  corporationCodes: []
                })
              }
            }
          })
        }
      }
      let params = {
        ...state.formData,
        allArea,
        areaCorporations
      }
      if (props.months == '') {
        exportTableData(state.tableId, params)
          .then((res) => {
            downLoadTemplate(res, props.tableTitle)
            state.isSubmiting = false
            handleClose()
          })
          .catch(() => {
            state.isSubmiting = false
          })
      } else {
        exportRepayTableData({ ...params, months: props.months })
          .then((res) => {
            downLoadTemplate(res, props.tableTitle)
            state.isSubmiting = false
            handleClose()
          })
          .catch(() => {
            state.isSubmiting = false
          })
      }
    }
  })
}
defineExpose({ state })
</script>

<style lang="scss" scoped></style>
